package com.treehouse.rpgdefender.system;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;

import com.artemis.Aspect;
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.annotations.Mapper;
import com.artemis.systems.EntityProcessingSystem;
import com.treehouse.rpgdefender.component.Body;
import com.treehouse.rpgdefender.component.Transformation;

/**
 * System class for rendering body components on the screen. May only be used in
 * testing mode in order to see collisions.
 * 
 * @author Julian Biermann
 * @version 26.02.2013 14:28:55 v1.0
 * 
 */
public class BodyRenderSystem extends EntityProcessingSystem {

	private GameContainer container;
	private Graphics graphics;
	@Mapper
	private ComponentMapper<Transformation> transformationMapper;
	@Mapper
	private ComponentMapper<Body> bodyMapper;

	/**
	 * Initialize the system.
	 * 
	 * @param container
	 *            Game container reference.
	 */
	@SuppressWarnings("unchecked")
	public BodyRenderSystem(GameContainer container) {
		super(Aspect.getAspectForAll(Transformation.class, Body.class));
		this.container = container;
		this.graphics = this.container.getGraphics();
	}

	@Override
	protected void initialize() {
		this.transformationMapper = world.getMapper(Transformation.class);
		this.bodyMapper = world.getMapper(Body.class);
	}

	@Override
	protected void process(Entity e) {
		Transformation transformation = this.transformationMapper.get(e);
		Body body = this.bodyMapper.get(e);

		// TODO: Set the body position in a seperate system? Overhaul the body
		// component maybe, because of the shape object being not really
		// compatible.
		body.getShape().setLocation(transformation.getX(),
				transformation.getY());

		graphics.setColor(Color.blue);
		graphics.draw(body.getShape());
	}

	@Override
	protected boolean checkProcessing() {
		return true;
	}
}
